package com.group4.DB;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.group4.DAO.VideoDAO;

public class Video {

	private int ID;
	private String videoName;
	private String videoUrl;
	private int videoUploaderID;
	private Date videoUploadDateTime;
	private String videoUploadLocation;
	private String videoDescription;
	private int videoRate;
	private String videoTag;

	static PreparedStatement ps = null;
	static ResultSet rs = null;
	
	public static ArrayList<VideoDAO> getVideosWithTag(String tag){
		ArrayList<VideoDAO> results = new ArrayList<VideoDAO>();
		try {
			ps = ConnectionManager.conn.prepareStatement("select * from  tbl_video where video_tag like ?");
			ps.setString(1, "%" + tag + "%");
			rs = ps.executeQuery();
			while(rs.next()){
				VideoDAO video = new VideoDAO();
				video.setID(rs.getInt("ID"));
				video.setVideoName(rs.getString("video_name"));
				video.setVideoUrl(rs.getString("video_url"));
				video.setVideoUploaderID(rs.getInt("video_uploaderId"));
				video.setVideoUploadDateTime(rs.getDate("video_uploadDateTime"));
				video.setVideoUploadLocation(rs.getString("video_uploadLocation"));
				video.setVideoDescription(rs.getString("video_description"));
				video.setVideoRate(rs.getInt("video_rate"));
				video.setVideoTag(rs.getString("video_tag"));
				results.add(video);
			}
		} catch (SQLException e) {
				// TODO Auto-generated catch block
			System.out.println("Error in getVideoWithTag");
			e.printStackTrace();
		}
		return results;
	}
	
	public static VideoDAO getVideoWithID(int ID) {

		if (ConnectionManager.conn == null) {
			return null;
		}

		VideoDAO video = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_video where ID = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				video = new VideoDAO();

				video.setID(ID);
				video.setVideoName(rs.getString("video_name"));
				video.setVideoUrl(rs.getString("video_url"));
				video.setVideoUploaderID(rs.getInt("video_uploaderId"));
				video.setVideoUploadDateTime(rs.getDate("video_uploadDateTime"));
				video.setVideoUploadLocation(rs
						.getString("video_uploadLocation"));
				video.setVideoDescription(rs.getString("video_description"));
				video.setVideoRate(rs.getInt("video_rate"));
				video.setVideoTag(rs.getString("video_tag"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getVideoWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getVideoWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getVideoWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoWithID. ");
					e.printStackTrace();
					
				}
			}
		}
		return video;
	}
	
	public static VideoDAO getVideoWithURL(String URL) {

		if (ConnectionManager.conn == null) {
			return null;
		}

		VideoDAO video = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_video where video_url = ?");

			ps.setString(1, URL);

			rs = ps.executeQuery();

			while (rs.next()) {

				video = new VideoDAO();

				video.setID(rs.getInt("ID"));
				video.setVideoName(rs.getString("video_name"));
				video.setVideoUrl(rs.getString("video_url"));
				video.setVideoUploaderID(rs.getInt("video_uploaderId"));
				video.setVideoUploadDateTime(rs.getDate("video_uploadDateTime"));
				video.setVideoUploadLocation(rs
						.getString("video_uploadLocation"));
				video.setVideoDescription(rs.getString("video_description"));
				video.setVideoRate(rs.getInt("video_rate"));
				video.setVideoTag(rs.getString("video_tag"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getVideoWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getVideoWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getVideoWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoWithID. ");
					e.printStackTrace();
					
				}
			}
		}
		return video;
	}
	
	public static List<VideoDAO> getVideos(int userId) {

		List<VideoDAO> videoList = new ArrayList<VideoDAO>();
		if (ConnectionManager.conn == null) {
			return null;
		}

		VideoDAO video = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_video where video_uploaderId = ?");

			ps.setInt(1, userId);

			rs = ps.executeQuery();

			while (rs.next()) {

				video = new VideoDAO();

				video.setID(rs.getInt("ID"));
				video.setVideoName(rs.getString("video_name"));
				video.setVideoUrl(rs.getString("video_url"));
				video.setVideoUploaderID(rs.getInt("video_uploaderId"));
				video.setVideoUploadDateTime(rs.getDate("video_uploadDateTime"));
				video.setVideoUploadLocation(rs
						.getString("video_uploadLocation"));
				video.setVideoDescription(rs.getString("video_description"));
				video.setVideoRate(rs.getInt("video_rate"));
				video.setVideoTag(rs.getString("video_tag"));
				videoList.add(video);
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getVideoWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getVideoWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getVideoWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoWithID. ");
					e.printStackTrace();
					
				}
			}
		}
		return videoList;
	}
	
	public static List<VideoDAO> getAllVideos() {

		List<VideoDAO> videoList = new ArrayList<VideoDAO>();
		if (ConnectionManager.conn == null) {
			return null;
		}

		VideoDAO video = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_video");

		

			rs = ps.executeQuery();

			while (rs.next()) {

				video = new VideoDAO();

				video.setID(rs.getInt("ID"));
				video.setVideoName(rs.getString("video_name"));
				video.setVideoUrl(rs.getString("video_url"));
				video.setVideoUploaderID(rs.getInt("video_uploaderId"));
				video.setVideoUploadDateTime(rs.getDate("video_uploadDateTime"));
				video.setVideoUploadLocation(rs
						.getString("video_uploadLocation"));
				video.setVideoDescription(rs.getString("video_description"));
				video.setVideoRate(rs.getInt("video_rate"));
				video.setVideoTag(rs.getString("video_tag"));
				videoList.add(video);
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getVideoWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getVideoWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getVideoWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getVideoWithID. ");
					e.printStackTrace();
					
				}
			}
		}
		return videoList;
	}
	
	
	public static boolean addVideo(VideoDAO video) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		try {
			ps = ConnectionManager.conn
					.prepareStatement(" insert into tbl_video (ID, video_name, video_url, video_uploaderId, video_uploadDateTime, video_uploadLocation, video_description, video_rate, video_tag)"
							+ " values (?, ?, ?, ?, ?, ?, ?, ?, ?)");

			ps.setInt(1, video.getID());
			ps.setString(2, video.getVideoName());
			ps.setString(3, video.getVideoUrl());
			ps.setInt(4, video.getVideoUploaderID());
			ps.setDate(5, video.getVideoUploadDateTime());
			ps.setString(6, video.getVideoUploadLocation());
			ps.setString(7, video.getVideoDescription());
			ps.setInt(8, video.getVideoRate());
			ps.setString(9, video.getVideoTag());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in addVideo. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in addVideo. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addVideo. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean updateVideo(VideoDAO video) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			VideoDAO oldVideo = getVideoWithID(video.getID());

			if (oldVideo == null) {
				System.out.println("No video found with ID = " + video.getID());
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("update tbl_video set video_name = ?, video_url = ?, video_uploaderId = ?, video_uploadDateTime = ?, video_uploadLocation = ?, video_description = ?, video_rate = ?, video_tag = ? where ID = ?");

			ps.setString(1, video.getVideoName());
			ps.setString(2, video.getVideoUrl());
			ps.setInt(3, video.getVideoUploaderID());
			ps.setDate(4, video.getVideoUploadDateTime());
			ps.setString(5, video.getVideoUploadLocation());
			ps.setString(6, video.getVideoDescription());
			ps.setInt(7, video.getVideoRate());
			ps.setString(8, video.getVideoTag());
			ps.setInt(9, video.getID());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in updateVideo. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in updateVideo. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addVideo. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean deleteVideo(int ID) {
		boolean result = false;
		
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		VideoDAO video = null;

		try {
			VideoDAO oldVideo = getVideoWithID(ID);

			if (oldVideo == null) {
				System.out.println("No video found with ID = " + video.getID());
				return result;
			}
			
			ps = ConnectionManager.conn.prepareStatement("delete from tbl_video where ID = ?");

			ps.setInt(1, ID);

			ps.execute();
			
			result = true;

		} catch (SQLException e) {
			System.out.println("SQL Error in deleteVideo. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in deleteVideo. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in deleteVideo. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Video [ID:").append(ID);
		sb.append("; video_name:").append(videoName);
		sb.append("; video_url:").append(videoUrl);
		sb.append("; video_uploadDateTime:").append(videoUploadDateTime);
		sb.append("; video_uploadLocation:").append(videoUploadLocation);
		sb.append("; video_description:").append(videoDescription);
		sb.append("; video_rate:").append(videoRate);
		sb.append("; video_tag:").append(videoTag);

		sb.append("]");

		return sb.toString();
	}
}
